<script setup>
// import router from "@/router";
import {computed, defineEmits, defineProps, onMounted, reactive, ref, watch} from "vue";
import {useRoute} from "vue-router";
import store from "@/store";
import {$ajax, apiUrl} from "@/utils/ajax";
import {ElMessageBox} from "element-plus";

const prop = defineProps({
  open_btn: {
    default: () => false,
    type: Boolean
  }
})
const emit = defineEmits(['menuHideShow'])
const hideMenuBtn = computed(() => {
  return prop.open_btn ? "Expand" : "Fold"
})
const route = useRoute()
const account = computed(() => store.state.account)
const modify = ref(false)
const modifyyPassword = reactive({
  oldPassword: "",
  newPassword: "",
  confirmPassword: ""
})
const modifyRefs = ref()
const modifyLoading = ref(false)
const modifyRule = reactive({
  oldPassword: [
    {
      required: true,
      message: "请输入旧密码",
      trigger: "blur"
    }
  ],
  newPassword: [
    {
      required: true,
      message: "请输入新密码",
      trigger: "blur"
    }
  ],
  confirmPassword: [
    {
      required: true,
      message: "请再次输入新密码",
      trigger: "blur"
    },
    {
      validator: (rule, value) => {
        return value === modifyyPassword.newPassword
      },
      message: "两次密码不一致",
      trigger: "blur"
    }
  ]
})
watch(route, (to) => {
  if (to.name !== 'login') store.dispatch("getUser")
})
onMounted(() => {
  if (route.name !== 'login') {
    store.dispatch("getUser")
  }
})
const handleCommand = (command) => {
  switch (command) {
    case 'modify':
      ElMessageBox.confirm("确认修改密码？", "修改密码", {type: 'warning'}).then(() => modify.value = true)
      break
    case 'logout':
      ElMessageBox.confirm("确认退出登录？", "退出", {type: 'warning'}).then(() => {
        store.commit("logout")
      })
      break
  }
}
const modifyFn = refs => {
  refs.validate(valid => {
    modifyLoading.value = valid
    if (valid) {
      $ajax.post(`${apiUrl}authorization/modify`, modifyyPassword).then(() => {
        modifyLoading.value = false
        store.commit("logout")
      }).catch(() => {
        modifyLoading.value = false
      })
    }
  })
}
</script>

<template>
  <div class="vea-header">
    <div class="vea-title">
      <div>智行九州管理系统</div>
    </div>
    <div class="vea-nav">
      <div>
        <el-button :icon="hideMenuBtn" size="small" plain @click="emit('menuHideShow')"/>
      </div>
      <div class="right-menu">
        <el-dropdown @command="handleCommand" class="vea-menu">
          <div class="vea-menu-title">
            <el-avatar icon="user" fit="cover"></el-avatar>
            <span style="padding-left: 10px">
<!--              {{ userInfo.surname || userInfo.username }}-->
              {{ account.account }}
            </span>
            <el-icon>
              <CaretBottom/>
            </el-icon>
          </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="modify" icon="edit">
                修改密码
              </el-dropdown-item>
              <el-dropdown-item command="logout" icon="switch-button">
                退出登录
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
  </div>
  <el-dialog title="修改密码" v-model="modify" width="45%">
    <el-form :model="modifyyPassword" ref="modifyRefs" :rules="modifyRule" label-width="100px" label-position="left">
      <el-form-item label="旧密码" prop="oldPassword">
        <el-input v-model="modifyyPassword.oldPassword" type="password" show-password placeholder="请输入旧密码"
                  clearable/>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input v-model="modifyyPassword.newPassword" type="password" show-password placeholder="请输入新密码"
                  clearable/>
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input v-model="modifyyPassword.confirmPassword" type="password" show-password placeholder="请再次输入新密码"
                  clearable/>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="modify = false">取消</el-button>
      <el-button type="primary" :loading="modifyLoading" @click="modifyFn(modifyRefs)">修改密码</el-button>
    </template>
  </el-dialog>
</template>

<style scoped lang="less">
.vea-header {
  height: 45px;
  background: var(--default-color);
  display: flex;
  align-items: center;
  //justify-content: space-between;
  box-sizing: border-box;

  .vea-title {
    box-sizing: border-box;
    padding: 10px;
    background: rgba(255, 255, 2255, .6);
    height: 100%;
    color: #ffffff;
    width: 220px;
    display: flex;
    //font-size: .8rem;
    align-items: center;
    justify-content: center;

    &.mobile-title {
      width: 100px;
    }

    .el-image {
      height: 100%;
    }
  }

  .vea-nav {
    display: flex;
    justify-content: space-between;
    width: calc(100% - 225px);
    margin-left: 5px;
    align-items: center;

    &.mobile-nav {
      width: calc(100% - 105px);
    }

    .vea-menu {
      padding: 10px;
      color: #fff;

      .vea-menu-title {
        display: flex;
        align-items: center;
      }
    }
  }

  .right-menu {
    display: flex;
    align-items: center;
    //color: #ffffff;

    .search-menu {
      display: flex;
      align-items: center;
      background: #ffffff;
      padding: 5px 20px;
      border-radius: 5px;
      color: var(--el-color-info);
      cursor: pointer;

      .el-icon {
        margin-right: 10px;
      }
    }
  }
}

</style>
